package client;

import controller.SocketServerController;
import service.NormalStore;
import service.Store;

import java.util.Objects;
import java.util.Scanner;

public class ClientCommandLine {
    public static void main(String[] args) {
//        System.out.println("Please input Data: <host> <port> <action> <key> [value]");
        String host = "localhost";
        int port = 12345;
        Client client = new SocketClient(host, port);

        Scanner scanner = new Scanner(System.in);
        System.out.println("Please Enter command:[ <set key value> | <get key> | <rm key>  | <exit> ]:");
        while (true) {
            System.out.print("> ");
            String command = scanner.nextLine();
            String[] parts = command.split(" ", 3);

            switch (parts[0]) {
                case "set":
                    if (parts.length == 3) {
                        client.set(parts[1], parts[2]);
                    } else {
                        System.out.println("Usage: set key value");
                    }
                    break;
                case "get":
                    if (parts.length == 2) {
                        System.out.println("Value: " + client.get(parts[1]));
                    } else {
                        System.out.println("Usage: get key");
                    }
                    break;
                case "rm":
                    if (parts.length == 2) {
                        System.out.println("If you decide to delete " + parts[1] + "?" + " \n[1]: Yes\n[Other]: No");
                        String flag = scanner.nextLine();
                        if(Objects.equals(flag, "1")) {
                            client.rm(parts[1]);
                        }else{
                            System.out.println("Tip: Remove is cancel!");
                        }
                    } else {
                        System.out.println("Usage: rm key");
                    }
                    break;
                case "exit":
                    System.out.println("Exiting...");
                    scanner.close();
                    System.exit(0);
                default:
                    System.out.println("Invalid command. \n Available commands: [ <set key value> | <get key> | <rm key>  | <exit> ]");
            }
        }
    }
}
